<template>
  <div id="app">
    <!-- 加载框 -->
    <div class="loading_box" v-if="loading">
      <v-progress-circular
          indeterminate
          color="primary"
      ></v-progress-circular>
    </div>
    <!-- 加载框 -->
    <div id="tools-bar">
      <div id="cancel" @click="cancel">取消</div>
      <div id="remark">评论</div>
      <div id="push" @click="save">发送</div>
    </div>
    <div class="bottomComment">
      <div class="login-input" style="width: 100%;height: 5vh">
        <input @focus="focusInput('user_name')" @blur="blurInput('user_name')" type="text" class="text-input" placeholder="评论" />
      </div>
      <div class="icon" @click="getWindowHeight"></div>
    </div>
    <!-- <div id="w1">
      <div id="avatar">
        <img :src="avatar"/>
      </div>
      <div>
        <div id="to-username-wrapper">
          @<span id="to">{{ comment.toUserName }}</span>
        </div>
        <div id="content-to-be-comment">
          {{ content }}
        </div>
      </div>
    </div> -->
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import avatar from '@/assets/banner/4.jpeg'

export default {
  name: "Comment",
  data() {
    return {
      height: 0,
      loading: false,
      avatar: avatar,
      topic: null,
      content: "",
      // 评论id
      comment: {
        topicId: "",
        fromUserId: "",
        toUserId: "",
        toUserName: "",
        content: ""
      }
    }
  }, mounted() {
    // type 评论类型
    let type = this.$store.state.comment.type;
    console.log("评论类型:" + type)
    // 评论帖子
    if (type == "topic") {
      this.topic = this.$store.state.comment.topic;
      this.content = this.topic.content;
      this.comment.topicId = this.topic.id;
      this.comment.fromUserId = this.$store.state.user.id;
      this.comment.toUserId = this.topic.userId;
      this.comment.toUserName = this.topic.userName;
    } else {
      // 评论评论
      console.log(this.$store.state.comment.comment);
      this.content = this.$store.state.comment.comment.content;
      this.comment.topicId = this.$store.state.comment.comment.topicId;
      this.comment.fromUserId = this.$store.state.user.id;
      this.comment.toUserId = this.$store.state.comment.comment.fromUserId;
      this.comment.toUserName = this.$store.state.comment.comment.toUserName;
      console.log(JSON.stringify(this.comment))
    }
  }, computed: {
    ...mapGetters({})
  }, methods: {
    getWindowHeight(){
      window.fullHeight = document.documentElement.clientHeight;
    },
    focusInput() {
      this.height = window.fullHeight;
    },
    blurInput() {
      this.height = window.fullHeight;
    },
    save() {
      this.loading = true;
      const url = this.global.community_server_url + '/topic/comment/save';
      console.log(url)
      this.$axios.post(url, this.comment).then((response) => {
        let code = response.data.code;
        if (code == 200) {
          console.log("评论成功")
          this.loading = false;
          // 清空上一次发帖缓存
          this.clearCache();
          this.$router.go(-1);
        } else {
          console.error(response);
        }
      }).catch((error) => {
        this.loading = false;
        console.log(error)
      })
    },
    cancel() {
      this.clearCache();
      this.$router.go(-1);
    },
    // 清空上一次发帖缓存数据
    clearCache() {
      this.$store.state.comment.type = "";
      this.$store.state.comment.topic = {};
      this.$store.state.comment.comment = {};
    }
  }
}
</script>

<style scoped>

/* 底部评论输入 */
.bottomComment{
  bottom: 0;
  position: fixed;
  left: 0;
  right:0;
  background: #f1f1f1;
  display: flex;
  padding: 1vh;
  height: 7vh;
  align-items: center;
}

.bottomComment .icon{
  height: 4vh;
  width: 4vh;
  background: gold;
  border-radius: 2vh;
}

.bottomComment .commentArea{
  flex: 1;
  background: #ffffff;
  border-radius: 0.7vh;
  margin-right: 1vh;
  height: 5vh;
}

.bottomComment input{
  font-size: 2vh;
  outline: none;
  height: 100%;
  width: 98%;
  margin-left: 2%;
}


.loading_box {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  z-index: 10000;
}

#tools-bar {
  display: flex;
  align-items: center;
  text-align: center;
  height: 7vh;
  padding: 0 2vh;
  font-size: 2vh;
  justify-content: space-between;
}

#cancel {
  /* width: 18vw; */
}

#remark {
  /* width: 64vw; */
  font-size: 2.4vh;
  font-weight: bold;
}

#push {
  /* width: 18vw; */
}

/*#textarea-wrapper {*/
/*  font-family: ArialMT, "Microsoft YaHei", Arial, sans-serif, Helvetica, STHeiti;*/
/*  font-size: x-large;*/
/*  font-weight: 200;*/
/*}*/



#w1 {
  width: 100%;
  display: flex;
  align-items: center;
  background: #f2f2f2;
}


#avatar img {
  width: 25vw;
  height: 25vw;
}


#to-username-wrapper {
  display: flex;
  align-items: center;
  justify-content: left;
  height: 50%;
  font-size: 6vw;
}

#content-to-be-comment {
  display: flex;
  align-items: center;
  justify-content: left;
  height: 50%;
  font-size: 6vw;
}


</style>